package practice_list;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-04-04
 * Time:13:54
 */
public class 二叉树展开为链表 {
    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<TreeNode>();
        preOrder(list,root);
        int size = list.size();
        for(int i = 1;i < size;i++){
            TreeNode cur = list.get(i-1);
            TreeNode prev = list.get(i);
            cur.left = null;
            cur.right = prev;
        }
    }

    public void preOrder(List<TreeNode> list,TreeNode root) {
        if (root != null) {
            list.add(root);
            preOrder(list,root.left);
            preOrder(list,root.right);
        }
    }
}
